package com.ruangong.GitClient.Test.GiteeAPITest;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruangong.GitClient.GiteeAPI.GiteeRepository;
import com.ruangong.GitClient.GiteeAPI.GiteeUser;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello, Gitee!");
        GiteeUser giteeUser = new GiteeUser("e751bc5f27b389245d1f32a630df020b","9649b65faee309bbae1eec463a973a4edfcb25e166ae6a57387cc55130a5b777");
        giteeUser.showInfo();
        OkHttpClient client =new OkHttpClient();
        String userName="SilverXQ";
        String url = "https://gitee.com/api/v5/user/repos?access_token="+giteeUser.getAccessToken()+"&type=all&sort=full_name&page=1&per_page=20";
//        String url2="https://gitee.com/api/v5/user/repos?access_token=c7024531e3dac960466431dace160cd7&type=all&sort=full_name&page=1&per_page=20";
        System.out.println(url);

        Request request = new Request.Builder()
                .url(url)
//                .header("Authorization","token "+giteeUser.getAccessToken())
                .build();

        try (Response response = client.newCall(request).execute()){
            if(response.isSuccessful()){
                assert response.body() != null;
                String jsonResponse = response.body().string();
                System.out.println(jsonResponse);
//                JSONArray jsonArray = new JSONArray(jsonResponse);
//                for (int i = 0; i < jsonArray.length(); i++) {
//                    JSONObject jsonObject = jsonArray.getJSONObject(i);
//                    // 假设你想获取每个仓库的名称和描述
//                    String name = jsonObject.getString("human_name");
//                    String description = jsonObject.optString("description", "No description provided.");
//                    System.out.println("Repository Name: " + name);
//                    System.out.println("Description: " + description);
//                    // 继续处理其他字段
//                    }

                Gson gson = new Gson();
                Type listType = new TypeToken<List<GiteeRepository>>(){}.getType();
                List<GiteeRepository> repos = gson.fromJson(jsonResponse, listType);

                for (GiteeRepository repo : repos) {
                    System.out.println("Repository Human_Name: " + repo.getHuman_name());
                    System.out.println("Repository Content: " + repo.getName());
                    System.out.println("Description: " + repo.getDescription());
                    System.out.println(repo);
                }


            }else{
                System.out.println("Failed to get response");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
